package com.lgs.extendabletextview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Build.VERSION;
import android.support.annotation.NonNull;
import android.support.v7.widget.AppCompatTextView;
import android.text.SpannableString;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.TextUtils.TruncateAt;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import com.lgs.extendabletextview.R.styleable;

public class ExtendableTextView extends AppCompatTextView {
    private String originText;
    private int leaveLines;
    private boolean isExpand;
    private String endText;
    private boolean underline;
    private String endTextOpen;
    private String endTextClose;
    OnGlobalLayoutListener listener;

    public ExtendableTextView(Context context) {
        this(context, (AttributeSet)null, 0);
    }

    public ExtendableTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ExtendableTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.originText = "";
        this.leaveLines = 2;
        this.isExpand = false;
        this.underline = false;
        this.endTextOpen = "<展开>";
        this.endTextClose = "<收缩>";
        String closeText = "";
        String openText = "";
        String myText = "";
        if (attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, styleable.CustomText);
            this.leaveLines = a.getInteger(styleable.CustomText_leaveLines, this.leaveLines);
            this.isExpand = a.getBoolean(styleable.CustomText_expand, false);
            closeText = a.getString(styleable.CustomText_close);
            openText = a.getString(styleable.CustomText_open);
            myText = a.getString(styleable.CustomText_text);
            a.recycle();
        }

        if (!TextUtils.isEmpty(closeText)) {
            this.endTextClose = closeText;
        }

        if (!TextUtils.isEmpty(openText)) {
            this.endTextOpen = openText;
        }

        if (TextUtils.isEmpty(myText)) {
            this.originText = this.getText().toString();
        } else {
            this.originText = myText;
            this.setText(this.originText);
        }

        this.endText = this.isExpand ? this.endTextClose : this.endTextOpen;
        this.setListener(context);
        this.addListener();
    }

    public void setText(String info) {
        this.originText = info;
        super.setText(this.originText);
    }

    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        this.removeListener();
    }

    private void removeListener() {
        if (VERSION.SDK_INT >= 16) {
            this.getViewTreeObserver().removeOnGlobalLayoutListener(this.listener);
        } else {
            this.getViewTreeObserver().removeGlobalOnLayoutListener(this.listener);
        }

    }

    private void addListener() {
        ViewTreeObserver observer = this.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(this.listener);
    }

    private void setListener(Context context) {
        this.listener = new OnGlobalLayoutListener() {
            public void onGlobalLayout() {
                int textLen;
                int endLen;
                if (ExtendableTextView.this.isExpand) {
                    textLen = ExtendableTextView.this.originText.length();
                    endLen = ExtendableTextView.this.endText.length();
                    CharSequence temp = ExtendableTextView.this.originText + ExtendableTextView.this.endText;
                    SpannableString spanContent = new SpannableString(temp);
                    spanContent.setSpan(new ClickableSpan() {
                        public void onClick(View view) {
                            ExtendableTextView.this.endText = ExtendableTextView.this.endTextOpen;
                            ExtendableTextView.this.isExpand = false;
                            ExtendableTextView.this.setText(ExtendableTextView.this.originText);
                            ExtendableTextView.this.addListener();
                        }

                        @SuppressLint({"ResourceAsColor"})
                        public void updateDrawState(TextPaint ds) {
                            super.updateDrawState(ds);
                            ds.setColor(Color.parseColor("#3F51B5"));
                            ds.setUnderlineText(ExtendableTextView.this.underline);
                        }
                    }, textLen, textLen + endLen, 33);
                    ExtendableTextView.this.setMovementMethod(LinkMovementMethod.getInstance());
                    ExtendableTextView.this.setHighlightColor(0);
                    ExtendableTextView.this.setText(spanContent);
                } else {
                    textLen = ExtendableTextView.this.getPaddingLeft();
                    endLen = ExtendableTextView.this.getPaddingRight();
                    float moreText = ExtendableTextView.this.getTextSize() * (float)ExtendableTextView.this.endText.length();
                    float ellipsizeEnd = ExtendableTextView.this.getTextSize();
                    String originText1 = ExtendableTextView.this.originText.replace("\n", " ");
                    float availableTextWidth = (float)((ExtendableTextView.this.getWidth() - textLen - endLen) * ExtendableTextView.this.leaveLines) - moreText - ellipsizeEnd;
                    TextPaint paint = ExtendableTextView.this.getPaint();
                    CharSequence ellipsizeStr = TextUtils.ellipsize(originText1, paint, availableTextWidth, TruncateAt.END);
                    if (ellipsizeStr.length() < originText1.length()) {
                        CharSequence tempx = ellipsizeStr + ExtendableTextView.this.endText;
                        SpannableString ssb = new SpannableString(tempx);
                        ssb.setSpan(new ClickableSpan() {
                            public void onClick(@NonNull View widget) {
                                ExtendableTextView.this.endText = ExtendableTextView.this.endTextClose;
                                ExtendableTextView.this.isExpand = true;
                                ExtendableTextView.this.setText(ExtendableTextView.this.originText);
                                ExtendableTextView.this.addListener();
                            }

                            @SuppressLint({"ResourceAsColor"})
                            public void updateDrawState(TextPaint ds) {
                                super.updateDrawState(ds);
                                ds.setColor(Color.parseColor("#3F51B5"));
                                ds.setUnderlineText(ExtendableTextView.this.underline);
                            }
                        }, tempx.length() - ExtendableTextView.this.endText.length(), tempx.length(), 33);
                        ExtendableTextView.this.setMovementMethod(LinkMovementMethod.getInstance());
                        ExtendableTextView.this.setHighlightColor(0);
                        ExtendableTextView.this.setText(ssb);
                    } else {
                        ExtendableTextView.this.setText(originText1);
                    }
                }

                ExtendableTextView.this.removeListener();
            }
        };
    }
}
